package cn.com.xy.sms.sdk.service.domainservice;

import android.text.TextUtils;
import android.util.LruCache;

import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import cn.com.xy.sms.sdk.db.entity.DomainDBManager;
import cn.com.xy.sms.sdk.log.LogManager;
import cn.com.xy.sms.sdk.threadpool.SmartSmsThreadPoolManager;
import cn.com.xy.sms.sdk.util.StringUtils;
import cn.com.xy.sms.sdk.util.ThreadPoolManager;
import cn.com.xy.sms.util.SdkCallBack;

/**
 * Created by kangwenbo on 2017/3/2.
 * <p>
 * jira : HUAWEI-2082
 */

public class DomainService {

    private final String TAG = "DomainService";
    private List<String> mList;
    private LruCache<String, List<String>> mItemCache = new LruCache<String, List<String>>(10);
    private static DomainService sDomainService;

    public static DomainService getInstance() {
        synchronized (DomainService.class) {
            if (sDomainService == null) {
                sDomainService = new DomainService();
            }
        }
        return sDomainService;
    }

    public void preLoadData(final String companyNum) {
        LogManager.e(TAG,"rid : " +companyNum);
        mList = mItemCache.get(companyNum);
        if (mList == null) {
            mList = new ArrayList<String>();
            mItemCache.put(companyNum, mList);
            SmartSmsThreadPoolManager.iccidPool.execute(new Runnable() {
                @Override
                public void run() {
                    mList = strToList(DomainDBManager.getInstance().queryDomainByCompanyNum(companyNum));
                    if (mList.size() == 0) {
                        // 去联网获取对应的企业id的域名资料
                        new DomainUpdateWhiteList().requestUpdateData(companyNum, new SdkCallBack() {
                            @Override
                            public void execute(Object... obj) {

                                if (obj != null && obj.length > 0) {
                                    mList = strToList((String) obj[0]);
                                    mItemCache.put(companyNum, mList);
                                } else {
                                    mItemCache.remove(companyNum);
                                }
                            }
                        });
                    } else{
                        mItemCache.put(companyNum, mList);
                    }
                    new DomainUpdateWhiteList().requestUpdate();
                }
            });
        }
    }

    public boolean isContain(String url, ArrayList<String> mList) {
        LogManager.e(TAG, "url :" + url);
        if (mList == null || mList.size() == 0) {
            LogManager.e(TAG, "not domain data");
            return true;
        }

        if (!StringUtils.isNull(url)) {
            try {
                URL httpUrl = new URL(url);
                String host = httpUrl.getHost();
                LogManager.e(TAG, host);
                for (String str : mList) {
                    if (host.contains(str)){
                       String newStr = host.replace(str,"");
                        if(newStr !=null && (TextUtils.isEmpty(newStr) || newStr.contains("..") || newStr.endsWith(".") || newStr.startsWith(".")))
                            return true;
                    }
                }
            } catch (Throwable e) {
                LogManager.e(TAG, "isContain", e);
            }
        }
        return false;
    }

    public List<String> getDomainList() {
        if (mList == null) {
            return new ArrayList<String>();
        } else {
            return mList;
        }
    }

    private List<String> strToList(String domain) {
        List<String> list = new ArrayList<String>();
        if (!StringUtils.isNull(domain)) {
            String array[] = domain.split(";");
            if (array != null) {
                for (int i =0;i<array.length;i++){
                    if(!StringUtils.isNull(array[i])){
                        list.add(array[i]);
                    }
                }
            }
        }
        return list;
    }
}
